package org.watchrecords.pages.crime;

import org.apache.commons.lang.ObjectUtils;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.watchrecords.constants.Permission;
import org.watchrecords.dbo.Incident;
import org.watchrecords.dbo.Offender;
import org.watchrecords.managers.IncidentManager;
import org.watchrecords.pages.BasicPage;
import org.watchrecords.util.FormatHelper;

import com.google.inject.Inject;

public class ViewIncidentsPage extends BasicPage {
    @Inject private IncidentManager incidentManager;
    
    public ViewIncidentsPage() {
        super(null);
        final boolean canEdit = haveAnyPermission(Permission.EDIT_INCIDENT);
        WebMarkupContainer linkList = new WebMarkupContainer("linkList");

        add(linkList);
        
        if(!canEdit) {
            linkList.setVisible(false);
        }
        
        add(new ListView<Incident>("incidentList", incidentManager.getIncidents()) {
            /**
             * 
             */
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(ListItem<Incident> item) {
                final Incident inc = item.getModelObject();
                
                item.add(new Label("district", ObjectUtils.toString(inc.getDistrict())));
                item.add(new Label("location", inc.getLocation()));
                item.add(new Label("date", inc.getDate() == null ? "" : FormatHelper.getDateFormat().format(inc.getDate())));
                item.add(new Label("victim", inc.getVictim()));
                
                item.add(new ListView<Offender>("offender", inc.getOffenders()) {
                    /**
                     * 
                     */
                    private static final long serialVersionUID = -4488578908572736658L;

                    @Override
                    protected void populateItem(ListItem<Offender> item) {
                        Offender off = item.getModelObject();
                        Link<String> link = new BookmarkablePageLink<String>("offenderLink", ViewOffenderPage.class,
                                new PageParameters("offenderId=" + off.getId()));
                        
                        link.add(new Label("offenderName", off.getFullName()));
                        item.add(link);
                    }
                });
                
                Link<Object> editLink = new BookmarkablePageLink<Object>("editLink", EditIncidentPage.class,
                        new PageParameters("incidentId=" + inc.getId()));
                
                Link<Object> deleteLink = new Link<Object>("deleteLink") {
                    private static final long serialVersionUID = 99269614290037130L;

                    @Override
                    public void onClick() {
                        incidentManager.deleteIncident(inc);
                        setResponsePage(ViewIncidentsPage.class);
                    }
                };
                
                item.add(editLink);
                item.add(deleteLink);
                
                if (!canEdit) {
                    editLink.setVisible(false);
                    deleteLink.setVisible(false);
                }
            }
            
        });
    }
}
